﻿using System;
using System.Runtime.InteropServices;
namespace MaiYao
{
    public static class 虚鼠_
    {
        [DllImport("user32.dll")]
        private static extern bool PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

        [DllImport("user32.dll")]
        private static extern bool SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

        [StructLayout(LayoutKind.Sequential)]
        public struct POINT
        {
            public int X;
            public int Y;

            public POINT(int x, int y)
            {
                this.X = x;
                this.Y = y;
            }
        }
        // 消息常量
        private const uint WM_KEYDOWN = 0x0100;
        private const uint WM_KEYUP = 0x0101;
        private const uint WM_CHAR = 0x0102;
        private const uint WM_LBUTTONDOWN = 0x0201;
        private const uint WM_LBUTTONUP = 0x0202;
        private const uint WM_MOUSEMOVE = 0x0200;
        private const uint WM_RBUTTONDOWN = 0x0204;
        private const uint WM_RBUTTONUP = 0x0205;


        private static double K;
        private static double G;

        static 虚鼠_()
        {
            K = System.Windows.SystemParameters.PrimaryScreenWidth;
            G = System.Windows.SystemParameters.PrimaryScreenHeight;
        }
        // 发送鼠标左键点击
        public static void SendMouseClick(IntPtr hWnd, int x, int y)
        {
            // 计算 lParam (低位是X坐标，高位是Y坐标)
            IntPtr lParam = (IntPtr)((y << 16) | x);

            // 发送鼠标按下和释放消息
            PostMessage(hWnd, WM_MOUSEMOVE, IntPtr.Zero, lParam);
            PostMessage(hWnd, WM_LBUTTONDOWN, new IntPtr(1), lParam);
            System.Threading.Thread.Sleep(50); // 稍微延迟，模拟真实点击
            PostMessage(hWnd, WM_LBUTTONUP, new IntPtr(0), lParam);
        }
    }
}
